package Concurrent.countDownLatch.notice.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wangyg6 on 2017/11/29.
 * 主线程做一个工作，然后one two three main顺序执行 ,@fixme 有死锁问题，比如主线程还没到await,线程三已经signal了
 * TODO 需要使用标志位 以及while或者for循环
 * condition实现
 */
public class ConditionDemo {

	public static void main(String[] args) {
		ReentrantLock lock = new ReentrantLock();
		Condition one = lock.newCondition();
		Condition two = lock.newCondition();
		Condition three = lock.newCondition();
		new Thread(new TaskThree(lock, two, three), "TaskThree Thread").start();
		new Thread(new TaskTwo(lock, one, two), "TaskTwo Thread").start();
		new Thread(new TaskOne(lock, one), "TaskOne Thread").start();
		try {
			lock.lock();
			three.await();
			Thread.sleep((long) (Math.random() * 1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		System.out.println("main Thread complete");
	}
}
